<?php

namespace common\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%message_config_item}}".
 *
 * @property integer $id
 * @property integer $config_id
 * @property integer $site_id
 * @property string $name
 * @property string $send_way
 * @property string $template_sys
 * @property string $template_wechat
 * @property string $template_mail
 * @property string $template_sms
 * @property integer $admin_id
 * @property integer $created_at
 */
class MessageConfigItem extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%message_config_item}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['config_id', 'send_way'], 'required'],
            [['config_id', 'site_id', 'admin_id', 'created_at'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['template_sys', 'template_wechat', 'template_mail', 'template_sms', 'template_jpush'], 'string', 'max' => 255]
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['send_way'] = function () {
            $send = unserialize($this->send_way);
            $arr = [];
            foreach ($send as $v) {
                switch ($v) {
                    case 'jpush':
                        $arr[$v] = \Yii::t('frontend', '极光推送');
                        continue;
                    case 'sys':
                        $arr[$v] = \Yii::t('frontend', '系统消息');
                        continue;
                    case 'email':
                        $arr[$v] = \Yii::t('frontend', '邮件消息');
                        continue;
                    case 'wechat':
                        $arr[$v] = \Yii::t('frontend', '微信消息');
                        continue;
                    case 'sms':
                        $arr[$v] = \Yii::t('frontend', '短信消息');
                        continue;
                }
            }
            return implode('|', $arr);

        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'config_id' => Yii::t('common', '配置编号'),
            'site_id' => Yii::t('common', '分站编号'),
            'name' => Yii::t('common', '标题'),
            'send_way' => Yii::t('common', '发送方式'),
            'template_sys' => Yii::t('common', '系统消息模板'),
            'template_wechat' => Yii::t('common', '微信消息模板'),
            'template_mail' => Yii::t('common', '邮件消息模板'),
            'template_sms' => Yii::t('common', '短信消息模板'),
            'template_jpush' => Yii::t('common', '极光推送消息模板'),
            'admin_id' => Yii::t('common', '管理员'),
            'created_at' => Yii::t('common', '创建时间'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
            'config_name' => function () {
                return isset($this->messageConfig->name)?$this->messageConfig->name:'';
            },
            'type_id' => function () {
                return isset($this->messageConfig->type_id)?$this->messageConfig->type_id:'';
            },
            'frontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();

            },

        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'messageconfigitem'
            ]
        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }

    public function getMessageConfig()
    {
        return $this->hasOne(MessageConfig::className(), ['id' => 'config_id']);
    }
}
